package com.david.easyExcel.easyExcelDemo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.david.easyExcel.model.ExcelData;

import java.util.*;

public class ExcelTest {
    public static void main(String[] args) {
        List<ExcelData> excelData = parseData();
        EasyExcel.write("F:\\pythonData\\用户数据表-副本.xlsx")
                .head(ExcelData.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet()
                .doWrite(excelData);

    }


    public static void ToObject(){
        List<Map<Integer,String>> list=new LinkedList<>();
        EasyExcel.read("F:\\pythonData\\用户数据表.xlsx")
                .sheet()
                .registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {
                    @Override
                    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                        list.add(integerStringMap);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("数据读取完毕");
                    }
                }).doRead();

        for (Map<Integer, String> integerStringMap : list) {
            Set<Integer> keySet = integerStringMap.keySet();
            Iterator<Integer> iterator = keySet.iterator();
            while (iterator.hasNext()) {
                Integer key = iterator.next();
                System.out.println(key+":"+integerStringMap.get(key)+", ");
            }
            System.out.println(" ");
        }

    }


    public static List<ExcelData> parseData(){
        List<ExcelData> list=new LinkedList<>();
        EasyExcel.read("F:\\pythonData\\用户数据表.xlsx")
                .head(ExcelData.class)
                .sheet()
                .registerReadListener(new AnalysisEventListener<ExcelData>() {

                    @Override
                    public void invoke(ExcelData excelData, AnalysisContext analysisContext) {
                        list.add(excelData);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("数据读取完毕");
                    }
                }).doRead();
        return list;
    }
}
